/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-10-21 16:53:38
 * @LastEditors: TT-hzy
 * @LastEditTime: 2024-08-17 11:11:17
 */

import 'package:flutter/foundation.dart';

import 'package:hzy_project_template/config/config_index.dart';
import 'package:hzy_project_template/controllers/user_c.dart';
import 'package:hzy_project_template/pages/tu_chong/pages/tu_chong_collect_list_v.dart';
import 'package:hzy_project_template/utils/utils_index.dart';

import '../models/tu_chong_model.dart';
import '../models/tu_mock_data.dart';
import '../networks/tu_chong_api_utils.dart';
import 'tu_chong_state.dart';

class TuChongVM {
  TuChongState state = TuChongState();

  /// 配置图虫列表数据
  configTuChongListData({
    int page = 1,
    bool isShowLoad = false,
  }) async {
    int? psid;
    if (state.feedList.isNotEmpty) {
      psid = state.feedList.last.postId;
    }
    if (page == 1) {
      state.feedList.clear();
    }
    List<TuChongItem> list = [];
    HzyNormalResponse res = HzyNormalResponse.success(
      netData: {},
    );
    if (kIsWeb) {
      /// 由于图虫Demo 接口不支持Web,只能模拟数据
      if (mockData.length > page) {
        res.response = mockData[page - 1];
      } else {
        res.response = {
          "is_history": false,
          "counts": 10,
          "feedList": [],
          "message": "添加成功",
          "more": true,
          "result": "SUCCESS"
        };
      }
    } else {
      res = await TuChongApiUtils.loadtuchongData(
        page: page,
        lastPostId: psid,
        showload: isShowLoad,
      );
    }
    if (res.ok) {
      TuChongSource tuChongSource = TuChongSource.fromJson(res.response!);
      list = tuChongSource.feedList ?? [];
    }
    state.feedList.addAll(list);
    state.pageState = HzyNormalTools.configPageState(
      allNum: state.feedList.length,
      networkNum: list.length,
    );
    return state;
  }

  /// 配置收藏列表
  configCollectListData() async {
    state.pageState = PageState.emptyDataState;
    if (!UserC.isLogin) {
      return state;
    }
    var res = await TuChongApiUtils.loadCollectListData();
    if (res.ok && res.data != null) {
      final List<TuChongItem>? feedList =
          res.data is List<Map<dynamic, dynamic>> ? <TuChongItem>[] : null;
      if (feedList != null) {
        for (final dynamic item in res.data) {
          if (item != null) {
            tryCatch(() {
              feedList
                  .add(TuChongItem.fromJson(asT<Map<String, dynamic>>(item)!));
            });
          }
        }
      }
      state.collecList = feedList ?? [];
      state.pageState = state.collecList.isEmpty
          ? PageState.emptyDataState
          : PageState.noMoreDataState;
    }
    return state;
  }

  /// 添加收藏
  configAddCollect({
    required TuChongItem tuChongItem,
  }) async {
    showLoad();
    var res = await TuChongApiUtils.loadAddCollect(
      data: tuChongItem.toJson(),
    );
    loadDismiss();
    showToastMsg(
      res.msg,
      type: res.ok ? 2 : 3,
    );
    if (res.ok && Get.isRegistered<TuChongCollectListC>()) {
      Get.put(TuChongCollectListC()).getNetWorkData();
    }
  }

  /// 删除收藏
  configDelectCollect({
    required TuChongItem tuChongItem,
  }) async {
    showLoad();
    var res = await TuChongApiUtils.loadDelectCollect(
      postId: tuChongItem.postId!,
    );
    loadDismiss();
    showToastMsg(
      res.msg,
      type: res.ok ? 2 : 3,
    );
    if (res.ok && Get.isRegistered<TuChongCollectListC>()) {
      Get.put(TuChongCollectListC()).getNetWorkData();
    }
  }
}
